package com.hmall.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.ItemService;
import com.hmall.item.util.PageDTO;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("itemService")
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements ItemService {
    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 分页查询商品信息
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageDTO<Item> getItems(Integer page, Integer size) {
        Page<Item> itemPage = new Page<>(page, size);
        itemMapper.selectPage(itemPage, null);
        List<Item> records = itemPage.getRecords();
        long total = itemPage.getTotal();
        return new PageDTO<>(total, records);
    }

    @Override
    public Item getItemsById(Long id) {
        return itemMapper.selectById(id);
    }

    @Override
    public void insertItem(Item item) {
        itemMapper.insert(item);
        rabbitTemplate.convertAndSend("hmall.topic", "hmall.insert", item);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        //请求参数id：商品id，status：商品状态 1-代表上架 2-代表下架

        Item item = new Item();
        LambdaUpdateWrapper<Item> updateWrapper = Wrappers.lambdaUpdate(Item.class);
        updateWrapper.eq(Item::getId, id);
        item.setStatus(status);
        itemMapper.update(item, updateWrapper);
        if (status == 1) {
            Item item1 = itemMapper.selectById(id);
            rabbitTemplate.convertAndSend("hmall.topic", "hmall.insert", item1);
        } else
            rabbitTemplate.convertAndSend("hmall.topic", "hmall.delete", id);
    }

    @Override
    public void updateItem(Item item) {
        LambdaUpdateWrapper<Item> updateWrapper = Wrappers.lambdaUpdate(Item.class);
        updateWrapper.eq(Item::getId, item.getId());
        itemMapper.update(item, updateWrapper);
        rabbitTemplate.convertAndSend("hmall.topic", "hmall.insert", item);
    }

    @Override
    public void deleteItem(Long id) {
        itemMapper.deleteById(id);
        rabbitTemplate.convertAndSend("hmall.topic", "hmall.delete", id);
    }

    /**
     * 修改库存数量
     */
    @Override
    public Integer modifyStock(Long itemId, Integer num) {
        Item item1 = itemMapper.selectById(itemId);

        LambdaUpdateWrapper<Item> lambdaUpdate = Wrappers.lambdaUpdate(Item.class);
        lambdaUpdate.ge(Item::getId, itemId).set(Item::getStock, item1.getStock() - num);
        
        return itemMapper.update(null, lambdaUpdate);
    }
}
